<?php include dirname(__FILE__).'/common/header.php'; ?>

<?php include dirname(__FILE__).'/common/menu.php'; ?>

var attr = {};
	
	var mainGrid = new ZM.EditorGridPanel({
		url: list.baseUrl + 'code/getCodeList',
		history:'code_id',
		onlyShow:true,
		region:'center',
		tbar:[{
			text:'新建',
			cls:'btn-show',
			iconCls:'icon-create',
			hidden: !Ext.permission['code/createCode'.toLowerCase()],
			handler:function() {
				var frm = new ZM.FormPanel({
					url: list.baseUrl + 'code/createCode',
					keySubmit:true,
					success:function(res) {
						ZM.bo.msg('成功!',res.detail);
						mainGrid.store.reload();
						win.close();
					},
					defaults:{xtype:'textfield',width:800},
					items:[{
						fieldLabel:'名称',
						name:'code_name',
						allowBlank:false
					},{
						xtype:'textarea',
						fieldLabel:'CSS',
						name:'code_content_css',
						plugins:[new ZM.tabKey()],
						height: 380
					},{
						xtype:'textarea',
						fieldLabel:'JS',
						name:'code_content_js',
						plugins:[new ZM.tabKey()],
						height: 380
					},{
						xtype:'textarea',
						fieldLabel:'说明',
						plugins:[new ZM.tabKey()],
						name:'code_desc'
					}]
				});
				var win = new ZM.PopUp({
					title:'新建代码',
					type:'l',
					height: 1000,
					items:frm
				});
				win.show();
			}
		}],
		//autoExpandColumn:'code_desc',
		columns:[
			{header:'ID',dataIndex:'code_id',hidden:true},
			{header:'名称',dataIndex:'code_name',width:180,filter:{}},
			//{header:'描述',dataIndex:'code_desc',id:'code_desc',filter:{},renderer:function(v){
			//	return Ext.util.Format.nl2br(Ext.util.Format.htmlEncode(v));
			//}},
			{header: "排序",xtype:'sortcolumn',url: 'code/editCodeSort',dataId:'code_id',dataIndex: 'code_sort'},
			{header:'最后修改人',dataIndex:'user_name',toggle:true},
			{header:'最后修改时间',dataIndex:'code_time_lastmodified',width:160,toggle:true},
			{header:'是否初始引用',dataIndex:'code_status',renderer:ZM.status,width:60},
			{header:'操作',dataIndex:'',renderer:function(val,m,r) {
					var str = '';
					str += Ext.rd('编辑','fireEditCode',r,'code/editCode');
					str += Ext.rd('预览','fireDetail',r,'code/getCodeList');
					str += Ext.rd('查看图片','fireImage',r,'code/getCodeImageList');
					str += '<a href="'+list.baseUrl+'code_canvas/'+r.json.code_id+'" target="_blank">[sprite]</a>';
					if(r.json.code_status == 1) {
						str += Ext.rd('设为非初始引用','fireDisableCode',r,'code/editCode');
					} else {
						str += Ext.rd('设为初始引用','fireEnableCode',r,'code/editCode');
					}
					str += Ext.rd('删除','fireDeleteCode',r,'code/editCode','red');
					return str;
				}
			}
		]
	});

	list.grid = {
		xtype:"container",
		region:'center',
		layout:'border',
		items:[mainGrid,{
			id:'item_detail',
			split:true,
			collapseMode:'mini',
			collapsible:true,
			layout:'fit',
			items:{xtype:'textarea',readOnly:true,style:'background:#fffdd6;'},
			title:'描述',
			region:'east',
			width:350,
			autoScroll:true
		}]
	};
	
	mainGrid.on('rowclick',function(grid,rowIndex,e) {
		var r = mainGrid.getStore().getAt(rowIndex);
		

		if(e.target.tagName == 'A') {
			return;
		}
		Ext.getCmp('item_detail').get(0).setValue(r.json.code_desc);
	});

	mainGrid.fireEditCode = function(r) {
		var r = r.json;
		var toClose = false;
		var frm = new ZM.FormPanel({
			url: list.baseUrl + 'code/editCode',
			tbar:[{
				text:'保存',
				cls:'btn-show',
				iconCls:'icon-save',
				hidden:!Ext.permission['code/editCode'.toLowerCase()],
				handler:function(){
					ZM.submitForm(frm);
				}
			}],
			buttons:null,
			focusField:false,
			params:{code_id:r.code_id
					,code_time_lastmodified:r.code_time_lastmodified},
			success:function(res) {
				ZM.bo.msg('成功!',res.detail);
				mainGrid.store.reload();
				frm.params.code_time_lastmodified = res.addon.code_time_lastmodified;
			},
			defaults:{xtype:'textfield',width:800},
			items:[{
					fieldLabel:'名称',
					name:'code_name',
					value:r.code_name,
					allowBlank:false
				},{
					xtype:'textarea',
					fieldLabel:'CSS',
					name:'code_content_css',
					value:r.code_content_css,
					plugins:[new ZM.tabKey()],
					height: 380
				},{
					xtype:'textarea',
					fieldLabel:'JS',
					name:'code_content_js',
					value:r.code_content_js,
					plugins:[new ZM.tabKey()],
					height: 380
				},{
					xtype:'textarea',
					fieldLabel:'说明',
					value:r.code_desc,
					plugins:[new ZM.tabKey()],
					name:'code_desc'
				}]
		});
		var win = new ZM.PopUp({
			title:'编辑代码['+r.code_name+']',
			type:'l',
			height: 1000,
			items:frm
		});
		win.show();
	}
	
	
	mainGrid.fireDeleteCode = function(r) {
		var r = r.json;
		Ext.Msg.confirm('确认?','确定要删除代码['+r.code_name+']?',function(btn) {
			if(btn == 'yes') {
				ZM.ajax({
					url:list.baseUrl + 'code/deleteCode',
					params:{code_id:r.code_id},
					mask:list.grid,
					success:function(res) {
						ZM.bo.msg('成功!',res.detail);
						mainGrid.store.reload();
					},
					failure:function(res) {
						ZM.bo.msg('失败!',res.detail);
					}
				});
			}
		});
	}

	mainGrid.fireEnableCode = function(r) {
		var r = r.json;
		Ext.Msg.confirm('确认?','确定要设置代码['+r.code_name+']为项目初始引用?',function(btn) {
			if(btn == 'yes') {
				ZM.ajax({
					url:list.baseUrl + 'code/enableCode',
					params:{code_id:r.code_id},
					mask:list.grid,
					success:function(res) {
						ZM.bo.msg('成功!',res.detail);
						mainGrid.store.reload();
					},
					failure:function(res) {
						ZM.bo.msg('失败!',res.detail);
					}
				});
			}
		});
	}

	mainGrid.fireDisableCode = function(r) {
		var r = r.json;
		Ext.Msg.confirm('确认?','确定要设置代码['+r.code_name+']为项目非初始引用?',function(btn) {
			if(btn == 'yes') {
				ZM.ajax({
					url:list.baseUrl + 'code/disableCode',
					params:{code_id:r.code_id},
					mask:list.grid,
					success:function(res) {
						ZM.bo.msg('成功!',res.detail);
						mainGrid.store.reload();
					},
					failure:function(res) {
						ZM.bo.msg('失败!',res.detail);
					}
				});
			}
		});
	}

	mainGrid.fireImage = function(r) {
		var r = r.json;
		
		list.pCode = r.code_id;
		var fileGrid = new ZM.GridPanel({
			url: list.baseUrl + 'code/getCodeImageList',
			onlyShow: true,
			bbar:null,
	        autoExpandColumn : 'path',
			columns:[
				{header:'文件名',dataIndex:'code_image_name',width:150},
				{header:'缩略图',dataIndex:'code_image_url',renderer:ZM.cellImage},
				{header:'文件路径',dataIndex:'code_image_html',id:'path'},
				{header:'最后修改时间',dataIndex:'code_image_time_lastmodified'},
				{header:'文件大小(kB)',dataIndex:'code_image_size',width:80,renderer:function(val){
	                if(val > 180){
	                    return '<span style="color:red;">' + val + '</span>';
	                }else{
	                    return val;
	                }
	            }},
				{header: "操作", dataIndex: '',width:190,renderer:function(v,p,r){
						var str = "";
						str += '<a id="_'+r.id+'" title="fireDetail" href="#">[查看图片]</a>';
						str += '<a href="/download.php?file='+r.json.code_image_url+'" target="_blank">[下载]</a>';
						
						if(r.json.code_image_name != 'sprite.png')
							str += Ext.rd('删除','fireRemoveFile',r,'code/removeCodeImage');
						
						return str;
					}
				}
			]
		});

		fileGrid.fireRemoveFile = function(r) {
			var r = r.json;
			Ext.Msg.confirm('确认?','确定要删除图片['+r.code_image_name+']?',function(btn){
				if(btn == 'yes') {
					ZM.ajax({
						url: list.baseUrl + 'code/removeCodeImage',
						params:{code_image_path:r.code_image_path},
						mask:fileGrid,
						success:function(res) {
							ZM.bo.msg('成功!',res.detail);
							fileGrid.store.reload();
						}
					});
				}
			});
		}
		

		
		fileGrid.fireDetail = function(r) {
			viewImageInWindow(this,r,{name:'code_image_name',url:'code_image_url'});
		}
		

		var win = new ZM.PopUp({
			tbar:[
				new Ext.form.FormPanel({
					url:list.baseUrl + 'code/addCodeImage',
					fileUpload:true,
					id:'form_upload_image',
					success:function(res) {
						ZM.bo.msg('成功!',res.detail);
						fileGrid.store.reload();
					},
					items:[
						{xtype:'hidden',name:'code_id',value:list.pCode},
						{
							xtype:'fileuploadfield',
							name:'code_image[]',
							buttonText:'上传文件',
							buttonOnly:true,
							hideLabel:true,
							allowBlank:false,
							listeners:{
								'fileselected':function(f,v) {
									var img = new Image();
									img.onload = function(){
										Ext.Msg.confirm('文件己存在','确定要上传覆盖?',function(btn) {
											if(btn == 'yes') {
												ZM.submitForm(Ext.getCmp('form_upload_image'));
											}
										});
									}
									img.onerror = function(){
										ZM.submitForm(Ext.getCmp('form_upload_image'));
									}
									img.src = '/uploads/code_image/'+list.pCode+'/'+f.fileInput.dom.value;
								}
							}
						}
					]
				})
			],
			type:'l',
			items:fileGrid
		}).show();
		fileGrid.store.baseParams = {code_id:r.code_id};
		fileGrid.store.load();
	}

	
	
	function viewImageInWindow(grid,r,fields){
		var curIndex = 0,
			name = fields.name,
			url = fields.url;
		var store = grid.getStore();
		for(var i = 0 ; i < store.getCount();i++) {
			if(store.getAt(i).id == r.id) {
				curIndex = i;
				break;
			}
		}
		var r = r.json;
		var win = new ZM.PopUp({
			title:'查看图片['+r[name]+']',
			tbar:[{
				text:'上一张',
				cls:'btn-show',
				id:'tb-prev',
				disabled: curIndex ==0,
				handler:function() {
					curIndex -= 1;
					var r = store.getAt(curIndex).json;
					Ext.getCmp('image-cpt').el.update(isImage(r[url])?'<img src="'+r[url]+'">':'非图片');
					win.setTitle('查看图片['+r[name]+']');
					if(curIndex == 0 ) {
						this.disable();
					}
					Ext.getCmp('tb-next').enable();
				}
			},'-',{
				text:'下一张',
				cls:'btn-show',
				id:'tb-next',
				disabled: curIndex ==store.getCount()-1,
				handler:function() {
					curIndex += 1;
					var r = store.getAt(curIndex).json;
					Ext.getCmp('image-cpt').el.update(isImage(r[url])?'<img src="'+r[url]+'">':'非图片');
					win.setTitle('查看图片['+r[name]+']');
					if(curIndex == store.getCount()-1 ) {
						this.disable();
					}
					Ext.getCmp('tb-prev').enable();
				}
			}],
			items:[
				{xtype:'container',
				id:'image-cpt',
				html:isImage(r[url])?'<img src="'+r[url]+'">':'非图片'
				}
			]
		}).show();
		function isImage(url) {
			var suffixes = ['.jpg','.png','.gif'];
			var name = url.substring(url.length-4,url.length);
			for(var i = 0 ; i < suffixes.length;i++) {
				if(suffixes[i] == name) return true;
			}
			return false;
		}
	}

	mainGrid.fireDetail = function(r) {
		var r = r.json;
		list.detail.show().ownerCt.doLayout();
		var html = '';
		if(r.code_content_js.length > 10) {
			html += '<script type="text/javascript">\n' +r.code_content_js+ '\n<\/script>\n';
		}
		if(r.code_content_css.length > 10) {
			html += '<style type="text/css">\n'+ r.code_content_css + '\n<\/style>\n';
		}
		Ext.getCmp('code_view_wrap').update('<pre>'+hljs.highlight('html', html).value+'<\/pre>');
	}

	list.detail = new ZM.detailPanel({
		items:[{region:'center',id:"code_view_wrap",autoScroll:true}]
	});
	
<?php include dirname(__FILE__).'/common/layout.php'; ?>
mainGrid.store.load();
<?php include dirname(__FILE__).'/common/footer.php'; ?>